package com.jushu.storbox.actions;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.database.ITeamSpaceDao;
import com.jushu.storbox.entities.TeamSpaceInfo;
import com.jushu.storbox.entities.TeamSpaceMemberInfo;
import com.jushu.storbox.service.ServiceFactory;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.vo.PagedList;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.CreateTeamSpaceRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.GetTeamSpaceRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.MyTeamSpaceRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.TeamSpaceMemberListRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.TeamSpaceMemberMessageRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.UpdataTeamSpaceMemberRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.UpdataTeamSpaceRequest;

public class TeamSpaceAction extends BaseAction{
	private final static String TAG = TeamSpaceAction.class.toString();

	public void createTeamSpaceFromServer(final Context context,
			final CreateTeamSpaceRequest request,final Handler messageHandler,
			final int dir) {
		LogUtil.i(TAG, "createTeamSpaceFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				TeamSpaceInfo teamspace = ServiceFactory.instance(context).getTeamSpaceService().createTeamSpaceFromServer(context, request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_SUCCESS_AND_REFRESH;
				resourceMsg.obj = teamspace;
				messageHandler.sendMessage(resourceMsg);
			}
		});

	}

	public void updateTeamSpaceFromServer(final Context context,
			final UpdataTeamSpaceRequest request,final Handler messageHandler,final int dir, final String teamRole) {
		LogUtil.i(TAG, "updateTeamSpaceFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				TeamSpaceInfo teamSpaceInfo=ServiceFactory.instance(context).getTeamSpaceService().updateTeamSpaceFromServer(context, request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_SUCCESS_AND_REFRESH;
				resourceMsg.obj = teamSpaceInfo;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

	public void deleteTeamSpaceFromServer(final Context context,
			final GetTeamSpaceRequest request,final Handler messageHandler, final int dir, final String teamRole) {
		LogUtil.i(TAG, "deleteTeamSpaceFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				ServiceFactory.instance(context).getTeamSpaceService().deleteTeamSpaceFromServer(context, request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_SUCCESS_AND_REFRESH;
//				resourceMsg.obj = teamSpaceInfo;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

	public void getTeamSpaceListFromServer(final Context context,final int pageNo, final Handler messageHandler) {
		LogUtil.i(TAG, "getTeamSpaceListFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				MyTeamSpaceRequest req = new MyTeamSpaceRequest();
				req.setLimit(ClientConfig.CLOUD_COUNT_PER_PAGE);
				req.setOffset(pageNo*ClientConfig.CLOUD_COUNT_PER_PAGE);
				req.setUserId(getOnwerId());
				PagedList<TeamSpaceInfo> pagedList = ServiceFactory.instance(context).getTeamSpaceService().getTeamSpaceListFromServer(context, req);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_LIST_COMPLETE;
				resourceMsg.obj = pagedList;
				messageHandler.sendMessage(resourceMsg);
			}

			@Override
			protected void onNetworkUnAvailable(Context context,
					Handler msgHandler) {
				super.onNetworkUnAvailable(context, msgHandler);
				
				ITeamSpaceDao teamDao = DAOFactory.instance(context).getTeamSpaceDao();
				List<TeamSpaceInfo> teamList = teamDao.getTeamSpacesList();
				PagedList<TeamSpaceInfo> pagedList = new PagedList<TeamSpaceInfo>();
				pagedList.setPagedList(teamList);
				pagedList.setTotal(teamList.size());
				
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_LIST_COMPLETE;
				resourceMsg.obj = pagedList;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}
	
	public void getMoreTeamSpaceListFromServer(final Context context,final int pageNo, final Handler messageHandler) {
		LogUtil.i(TAG, "getTeamSpaceListFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				MyTeamSpaceRequest req = new MyTeamSpaceRequest();
				req.setLimit(ClientConfig.CLOUD_COUNT_PER_PAGE);
				req.setOffset(pageNo*ClientConfig.CLOUD_COUNT_PER_PAGE);
				req.setUserId(getOnwerId());
				PagedList<TeamSpaceInfo> pagedList = ServiceFactory.instance(context).getTeamSpaceService().getTeamSpaceListFromServer(context, req);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_LIST_COMPLETE;
				resourceMsg.obj = pagedList;
				messageHandler.sendMessage(resourceMsg);
			}

			@Override
			protected void onNetworkUnAvailable(Context context,
					Handler msgHandler) {
				super.onNetworkUnAvailable(context, msgHandler);
				
				List<TeamSpaceInfo> teamList = new ArrayList<TeamSpaceInfo>();
				PagedList<TeamSpaceInfo> pagedList = new PagedList<TeamSpaceInfo>();
				pagedList.setPagedList(teamList);
				pagedList.setTotal(teamList.size());
				
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_LIST_COMPLETE;
				resourceMsg.obj = pagedList;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

	public void getTeamSpaceListFromLocal(final Context context,final Handler messageHandler) {
		LogUtil.i(TAG, "getTeamSpaceListFromLocal[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				List<TeamSpaceInfo> teamSpaceList = ServiceFactory.instance(context).getTeamSpaceService()
						.getTeamSpaceListFromLocal(context);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_LIST_COMPLETE;
				resourceMsg.obj = teamSpaceList;
				messageHandler.sendMessage(resourceMsg);
			}
		});

	}

	public void getTeamSpaceMessageFromServer(final Context context,
			final GetTeamSpaceRequest request,final Handler messageHandler,
			final boolean netState, final int dir) {
		LogUtil.i(TAG, "getTeamSpaceMessageFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				TeamSpaceInfo list = ServiceFactory.instance(context).getTeamSpaceService().getTeamSpaceMessageFromServer(
						context, request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_GET_USER_MESSAGE;
				resourceMsg.obj = list;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

	public void getTeamSpaceMemberMessageFromServer(final Context context,
			final TeamSpaceMemberMessageRequest request,final Handler messageHandler,
			final int dir, final String teamRole) {
		LogUtil.i(TAG, "getTeamSpaceMemberMessageFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				TeamSpaceMemberInfo teamSpaceMemberInfo = ServiceFactory.instance(context).getTeamSpaceMemberService()
						.getTeamSpaceMemberMessageFromServer(context,
								request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_GET_USER_MESSAGE;
				resourceMsg.obj = teamSpaceMemberInfo;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

	public void updateTeamSpaceMemberMessageFromServer(final Context context,
			final UpdataTeamSpaceMemberRequest request,final Handler messageHandler,
			final int dir, final String teamRole) {
		
		LogUtil.i(TAG, "updateTeamSpaceMemberMessageFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				TeamSpaceMemberInfo info = ServiceFactory.instance(context).getTeamSpaceMemberService()
						.updateTeamSpaceMemberMessageFromServer(context,
								request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_GET_USER_MESSAGE;
				resourceMsg.obj = info;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

	public void getTeamSpaceMemberListFromServer(final Context context,
			final TeamSpaceMemberListRequest request,final Handler messageHandler,
			final int dir) {
		LogUtil.i(TAG, "getTeamSpaceMemberListFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				List<TeamSpaceMemberInfo> list = ServiceFactory.instance(context).getTeamSpaceMemberService()
						.getTeamSpaceMemberListFromServer(context, request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_GET_USER_MESSAGE;
				resourceMsg.obj = list;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

	public void deleteTeamSpaceMemberFromServer(final Context context,
			final TeamSpaceMemberMessageRequest request,final Handler messageHandler,
			final int dir, final String teamRole) {
		LogUtil.i(TAG, "deleteTeamSpaceMemberFromServer[Actions]");
		executeRunTask(new RealTaskRunnable(context, messageHandler) {
			protected void doTask(Context context, Handler msgHandler) throws ClientException {
				ServiceFactory.instance(context).getTeamSpaceMemberService().deleteTeamSpaceMemberFromServer(context, request);
				Message resourceMsg = new Message();
				resourceMsg.what = MessageCode.TEAMSPACE_GET_USER_MESSAGE;
//				resourceMsg.obj = list;
				messageHandler.sendMessage(resourceMsg);
			}
		});
	}

}
